
var isClick = true;
$(document).ready(function () {

//        setInitOptions('2');

    var swiper = new Swiper('.swiper-container', {
        pagination: {
            el: '.swiper-pagination'
        },
//            autoplay: true,
        loop: true
    });


    /** 获取ip定位省市 */
    globalData.ipPos = {
        city: "深圳市",
        city_id: 440300,
        county: "南山区",
        county_id: 440305,
        prov: "广东省",
        prov_id: 440000,
        addr: ""
    };
    globalData.selPos = {
        city: "深圳市",
        city_id: 440300,
        county: "南山区",
        county_id: 440305,
        prov: "广东省",
        prov_id: 440000,
        addr: ""
    };
    ajax_post("/Ip/getIpInfo", null, {
        succ: function succ(json) {
            globalData.ipPos = json.ip_info;
            globalData.selPos = json.ip_info;
            $('.citySelect').html(json.ip_info.city);
        },
        fail: function fail(data) {
        }
    });

    $('.inq-mobile').focus(function () {
        $('.error-inq').hide();
    });
});
//询价提交电话号码
function submit(e,obj,clickBtn){
    // 百度事件统计
    trackBaidu(1);

    var mobile, city, prov, sem,errorTip, cate = 1;

    if(clickBtn == 1){
        mobile = $('#phone').val().trim();
        errorTip = $('#inquiry .errorTip');
    }else if(clickBtn == 3){
        mobile = $('.inq-mobile').val().trim();
        errorTip = $('.error-inq');
    }

    if (mobile != '') {
        if (!checkPhone(mobile)) {
            errorTip.text("请填写正确的手机号").show();
            return false;
        }
    } else {
        errorTip.text("手机号不能为空").show();
        return false;
    }

    city = globalData.selPos.city;
    prov = globalData.selPos.prov;
    sem = globalData.sem;
    var inquiryInfo = {
        cate: cate,
        mobile: mobile,
        city: city,
        sem: sem,
        prov: prov
    };
    $.each(inquiryInfo, function (key, value) {
        if (!value) {
            delete inquiryInfo[key];
        }
    });
    // 百度事件统计
    trackBaidu(2);

    if(!isClick){
        return;
    }else{
        isClick = false;
    }
    postDraft(inquiryInfo, {
        succ: function succ() {
            // 百度事件统计
            trackBaidu(3,clickBtn);
            isClick = true;
            $(".submit-success").fadeIn(300);
        },
        fail:function(error){
            isClick = true;
            var msg = error.msg || "提交失败，请重试";
            alert(msg)
        }
    });
}


// 案例弹窗 按钮点击事件
function caseBtn() {
    var mobile = $('#phone2').val().trim();
    var errorTip = $('#casePage .errorTip');
    // 如果存在正确的电话号码，则提交
    if(mobile != ''){
        if (!checkPhone(mobile)) {
            errorTip.text("请填写正确的手机号").show();
            return false;
        }
        var city, prov, sem, cate = 1;

        city = globalData.selPos.city;
        prov = globalData.selPos.prov;
        sem = globalData.sem;
        var inquiryInfo = {
            cate: cate,
            mobile: mobile,
            city: city,
            sem: sem,
            prov: prov
        };
        $.each(inquiryInfo, function (key, value) {
            if (!value) {
                delete inquiryInfo[key];
            }
        });
        // 百度事件统计
        trackBaidu(2);

        if(!isClick){
            return;
        }else{
            isClick = false;
        }
        postDraft(inquiryInfo, {
            succ: function succ() {
                // 百度事件统计
                trackBaidu(3,2);
                isClick = true;
                $(".submit-success").fadeIn(300);
            },fail:function(error){
                isClick = true;
                var msg = error.msg || "提交失败，请重试";
                alert(msg)
            }
        });
        $('#casePage').fadeOut(300);
    }else{
        $('#casePage').fadeOut(300);
    }

}
function closeSuccessPanel() {
    $('.submit-success').fadeOut(300)
}
// 点击输入框展开备选项
function openOptions(e,obj) {
    e.preventDefault();
    $(obj).parents('.choose-pane').find('.options').slideToggle(300);
    $(obj).next('.icon-circle-down').toggleClass('rotate');
}
// 点击备选项收齐下拉框并修改数据
function chooseOption(e,obj,type) {
    if(e.target === obj){
        $(obj).slideToggle(300);
        $(obj).parents('.choose-pane').find('.icon-circle-down').toggleClass('rotate');
    }else{
        $(obj).slideToggle(300);
        $(obj).parents('.choose-pane').find('.icon-circle-down').toggleClass('rotate');
        var val = e.target.innerHTML;
        $(obj).parents('.choose-pane').find('.value-div').html(val);


    }
}
function removeSelf(e,obj) {
    $(obj).hide();
    $(obj).prev('input').focus();
}

// 显示报价弹窗
function showInquiryModal() {
    $('#inquiryModal').fadeIn(300);
}
function hideSelf(obj,e) {
    if(obj == e.target){
        $(obj).fadeOut(300)
    }
}
function closeModal() {
    $('#inquiryModal').fadeOut(300)
}

// 显示案例弹窗
var caseData = [
    {
        name:"户外广告屏",
        price:"450000",
        span:"间距P10 &nbsp;&nbsp; 面积106㎡",
        time:"2016/09/05建于郑州银行濮阳分行",
        img: "img/case1.jpg"
    },
    {
        name:"门头广告屏",
        price:"4200",
        span:"间距P8 &nbsp;&nbsp; 面积为2㎡",
        time:"2017/06/25建于南山科技园川菜馆",
        img: "img/case2.jpg"
    },
    {
        name:"户外广告屏",
        price:"1500000",
        span:"间距P6 &nbsp;&nbsp; 面积为100㎡",
        time:"2016/10/12建于山东鲁能泰山7号体育公园",
        img: "img/case3.jpg"
    },
    {
        name:"室内高清屏",
        price:"350000",
        span:"间距P1.5 &nbsp;&nbsp; 面积为8.37㎡",
        time:"2016/06/16建于深圳腾讯安全指挥中心",
        img: "img/case4.jpg"
    },
    {
        name:"室内高清屏",
        price:"2500000",
        span:"间距P2.5 &nbsp;&nbsp; 面积为172㎡",
        time:"2016/1/23建于浙江桐乡巨石集团展示厅",
        img: "img/case5.jpg"
    }
];
function openCaseModal(index) {
    var item = caseData[index];
    var modal = $('#casePage');
    modal.find('.name').html(item.name);
    modal.find('.price .main-color').html(item.price);
    modal.find('.span-size').html(item.span);
    modal.find('.time-add').html(item.time);

    modal.fadeIn(300);
}

// 打开城市选择
function openCitySelect() {
    $('#CitySelector').show();
    setTimeout(function () {
        $(".nav-bar").css("visibility","visible");
    },300)
}
// 关闭城市选择
function closeCityPanel() {
    $(".nav-bar").css("visibility","hidden");
    $('#CitySelector').hide();
}

// 部分随机数据
(function () {
    var array =[5,8,3,2,1,2,8,25,55,59,78,64,63,55,73,93,78,71,54,51,49,40,21,7];
    var hours = new Date().getHours();
    var totalNumber=0;
    for(var i=0;i<=hours;i++){
        totalNumber = array[i] + totalNumber;
    }
    $('.now-number').html(totalNumber);

})();

// 城市列表页
(function () {
    var cityList = [{"letter":"A","list":["阿拉善盟","鞍山市","安庆市","安阳市","阿坝藏族羌族自治州","安顺市","阿里地区","安康市","阿克苏地区","阿勒泰地区","阿拉尔市","澳门特别行政区"]},{"letter":"B","list":["北京市","保定市","包头市","巴彦淖尔市","本溪市","白山市","白城市","蚌埠市","亳州市","滨州市","北海市","百色市","白沙黎族自治县","保亭黎族苗族自治县","巴中市","毕节市","保山市","宝鸡市","白银市","博尔塔拉蒙古自治州","巴音郭楞蒙古自治州","北屯市"]},{"letter":"C","list":["承德市","沧州市","长治市","赤峰市","长春市","常州市","滁州市","池州市","长沙市","常德市","郴州市","潮州市","崇左市","澄迈县","昌江黎族自治县","成都市","楚雄彝族自治州","昌都市","昌吉回族自治州"]},{"letter":"D","list":["大同市","大连市","丹东市","大庆市","大兴安岭地区","东营市","德州市","东莞市","儋州市","东方市","定安县","德阳市","达州市","大理白族自治州","德宏傣族景颇族自治州","迪庆藏族自治州","定西市"]},{"letter":"E","list":["鄂尔多斯市","鄂州市","恩施土家族苗族自治州"]},{"letter":"F","list":["抚顺市","阜新市","阜阳市","福州市","莆田市","抚州市","佛山市","防城港市"]},{"letter":"G","list":["赣州市","广州市","桂林市","贵港市","广元市","广安市","甘孜藏族自治州","贵阳市","甘南藏族自治州","果洛藏族自治州","固原市"]},{"letter":"H","list":["邯郸市","衡水市","呼和浩特市","呼伦贝尔市","葫芦岛市","哈尔滨市","鹤岗市","黑河市","淮安市","杭州市","湖州市","合肥市","淮南市","淮北市","黄山市","菏泽市","鹤壁市","黄石市","黄冈市","衡阳市","怀化市","惠州市","河源市","贺州市","河池市","海口市","红河哈尼族彝族自治州","汉中市","海东市","海北藏族自治州","黄南藏族自治州","海南藏族自治州","海西蒙古族藏族自治州","哈密市","和田地区"]},{"letter":"J","list":["晋城市","晋中市","锦州市","吉林市","鸡西市","佳木斯市","嘉兴市","金华市","景德镇市","九江市","吉安市","济南市","济宁市","焦作市","济源市","荆门市","荆州市","江门市","揭阳市","嘉峪关市","金昌市","酒泉市"]},{"letter":"K","list":["开封市","昆明市","克拉玛依市","克孜勒苏柯尔克孜自治州","喀什地区","可克达拉市","昆玉市"]},{"letter":"L","list":["廊坊市","临汾市","吕梁市","辽阳市","辽源市","连云港市","丽水市","六安市","龙岩市","莱芜市","临沂市","聊城市","洛阳市","娄底市","柳州市","来宾市","临高县","乐东黎族自治县","陵水黎族自治县","泸州市","乐山市","凉山彝族自治州","六盘水市","丽江市","临沧市","拉萨市","林芝市","兰州市","陇南市","临夏回族自治州"]},{"letter":"M","list":["牡丹江市","马鞍山市","茂名市","梅州市","绵阳市","眉山市"]},{"letter":"N","list":["南京市","南通市","宁波市","南平市","宁德市","南昌市","南阳市","南宁市","内江市","南充市","怒江傈僳族自治州","那曲地区"]},{"letter":"P","list":["盘锦市","萍乡市","平顶山市","濮阳市","攀枝花市","普洱市","平凉市"]},{"letter":"Q","list":["秦皇岛市","齐齐哈尔市","七台河市","衢州市","泉州市","青岛市","潜江市","清远市","钦州市","琼海市","琼中黎族苗族自治县","黔西南布依族苗族自治州","黔东南苗族侗族自治州","黔南布依族苗族自治州","曲靖市","庆阳市"]},{"letter":"R","list":["日照市","日喀则市"]},{"letter":"S","list":["石家庄市","朔州市","沈阳市","四平市","松原市","双鸭山市","绥化市","上海市","苏州市","宿迁市","绍兴市","宿州市","厦门市","三明市","上饶市","三门峡市","商丘市","十堰市","随州市","神农架林区","邵阳市","韶关市","深圳市","汕头市","汕尾市","三亚市","三沙市","遂宁市","山南市","商洛市","石嘴山市","石河子市","双河市"]},{"letter":"T","list":["天津市","唐山市","太原市","通辽市","铁岭市","通化市","泰州市","台州市","铜陵市","泰安市","漯河市","天门市","屯昌县","铜仁市","铜川市","天水市","吐鲁番市","塔城地区","图木舒克市","铁门关市","台湾省"]},{"letter":"W","list":["乌海市","乌兰察布市","无锡市","温州市","芜湖市","潍坊市","威海市","武汉市","梧州市","五指山市","文昌市","万宁市","文山壮族苗族自治州","渭南市","武威市","吴忠市","乌鲁木齐市","五家渠市"]},{"letter":"X","list":["邢台市","忻州市","兴安盟","锡林郭勒盟","徐州市","宣城市","新余市","新乡市","许昌市","信阳市","襄阳市","孝感市","咸宁市","仙桃市","湘潭市","湘西土家族苗族自治州","西双版纳傣族自治州","西安市","咸阳市","西宁市","香港特别行政区"]},{"letter":"Y","list":["阳泉市","运城市","营口市","延边朝鲜族自治州","伊春市","盐城市","扬州市","鹰潭市","宜春市","烟台市","宜昌市","岳阳市","益阳市","永州市","阳江市","云浮市","玉林市","宜宾市","雅安市","玉溪市","延安市","榆林市","玉树藏族自治州","银川市","伊犁哈萨克自治州"]},{"letter":"Z","list":["张家口市","朝阳市","镇江市","舟山市","漳州市","淄博市","枣庄市","郑州市","周口市","驻马店市","株洲市","张家界市","珠海市","湛江市","肇庆市","中山市","重庆市","自贡市","资阳市","遵义市","昭通市","张掖市","中卫市"]}];
    var CityIndex = ["A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","W","X","Y","Z"];
    var CityHots = ["北京市","长春市","常州市","长沙市","大连市","贵阳市","广州市","哈尔滨市","济南市","昆明市","宁波市","青岛市","上海市","深圳市","无锡市","西安市","厦门市","郑州市"];
    // 渲染热门城市
    var hotDom = '';
    $.each(CityHots,function (index, value) {
        hotDom += '<div class="city-button" data-city="'+value+'">'+value+'</div>';
    });
    $('.line-1').html(hotDom);

    //渲染城市列表
    var cityDom = '';
    $.each(cityList,function (index,item) {
        var letter = '<div class="city-part" id="CitySelector-'+item.letter +'"><span class="city-list">'+item.letter +'</span>';
        var citys = '';
        $.each(item.list,function (i, city) {
            citys += '<div class="city-item" data-city="'+city+'">'+city+'</div>';
        });
        cityDom += letter + citys + '</div>';
    });
    $('.city-all').html(cityDom);

    // 渲染字母表
    var letDom = '';
    $.each(CityIndex,function (index, value) {
        letDom += '<a href="#CitySelector-'+value+'">'+value+'</a>';
    });
    $('.nav-bar').append(letDom);
})();
// 城市选择事件
function selected(e){
    var selCity = e.target.getAttribute('data-city');
    if(selCity){
        globalData.selPos.city = selCity;
        globalData.selPos.prov = globalData.cityToProvince[selCity];
        $(".nav-bar").css("visibility","hidden");
        $('#CitySelector').hide();
        $(".citySelect").html(selCity);
        $(".currCity").html(selCity);
    }
}

/** 百度统计事件
 * type: 1 ==  点击按钮就计算一次
 * type: 2 ==  手机号正确并点击按钮
 * type: 3 ==  手机号正确并提交草稿成功
 * */
/** 百度统计事件
 * type: 1 ==  点击按钮就计算一次
 * type: 2 ==  手机号正确并点击按钮
 * type: 3 ==  手机号正确并提交草稿成功
 * */
function trackBaidu(type,pos) {
    var src = 'http://m.dapinghui.com/track/Click_Btn.html';

    if (type == 2) {
        src = 'http://m.dapinghui.com/track/Click_With_Mobile.html';
    } else if (type == 3) {
        if(pos == 2){
            src = 'http://m.dapinghui.com/track/Click_And_Submit_Case.html';
        }else if(pos == 3){
            src = 'http://m.dapinghui.com/track/Click_And_Submit_Bottom.html';
        }else{
            src = 'http://m.dapinghui.com/track/Click_And_Submit_Main.html';
        }

    } else if (type == 4) {
        src = 'http://m.dapinghui.com/track/Click_MeiQia.html';
    } else if (type == 5) {
        src = 'http://m.dapinghui.com/track/Click_Call.html';
    }

    // 百度事件统计
    var iframe = '<div class="hide"><iframe src="' + src + '"></iframe>"</div>';

    $('body').append(iframe);

}


function checkPhone(phone) {
    if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone)) {
        return false;
    }
    return true;
}

// 点击电话按钮统计事件
function call() {
    trackBaidu(5);
}

// 提交需求草稿
function postDraft(data, cb) {
    ajax_post("/Client/Demand/postDraft", data, cb);
}

/** ajax请求方法 */
function ajax_post(url, data, cb) {
    var URL = "https://opc.wanpinghui.com/api" + url;
    $.ajax(URL, $.extend({
        method: 'POST',
        data: data,
        dataType: "json",
        timeout: 30000,
        xhrFields: {
            withCredentials: true
        },
        headers: {
            'Accept': 'application/x-www-form-urlencoded',
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        contentType: 'application/x-www-form-urlencoded',
        success: function success(json, status, xhr) {
            cb && cb.always && cb.always(json);
            if (json.status == "200") {
                cb && cb.succ && cb.succ(json);
            } else {
                cb && cb.fail && cb.fail(json);
            }
        },
        error: function error(xhr, status, thrown) {
            console.log("[!err!](" + url + "): status: " + xhr.status + ", msg: " + thrown);
            cb && cb.fail && cb.fail();
            console.error("接口[" + url + "]：失败");
        }
    }));
}